<?php
namespace Admin\Model;
use Think\Model;
class CatsModel extends Model {
	//声明一个生成分级下拉列表的方法
	function formSelect(){
		//查询商品分类
		$cats=M('cats');
		$allCats=$cats->field("catsid,pid,catsname,des,concat(path,'-',catsid) as abspath")->order('abspath')->select();
		// dump($allCats);
		//<!--计算路径中-的个数-->
		//循环生成select标签的字符串 返回
		$selectStr='<select name="goodscats">';
		foreach($allCats as &$cats){
			//计算分级等级
			$cats['level']=substr_count($cats['abspath'],'-');
			//添加分级缩进
			$cats['subfix']=str_repeat('&nbsp;&nbsp;&nbsp;',$cats['level']);
			//添加分级颜色
			//顶级类不能添加商品
			if($cats['level']==1){
				$cats['bgcolor']='#ccc';
				$disabled='disabled';
			}elseif($cats['level']==2) {
				$cats['bgcolor']='yellow';
				$disabled='';
			}else{
				$cats['bgcolor']='CYAN';
				$disabled='';
			}	
			//生成select标签字符串
			$selectStr.='<option style="background:'.$cats['bgcolor'].'" value="'.$cats['catsname'].'"'.$disabled.'>'.$cats['subfix'].$cats['catsname'].'</option>';
		}
		$selectStr.='</select>';
		return $selectStr;
	}
}